package com.chaoyong.higo.fragment;

import java.util.List;

import org.apache.http.entity.StringEntity;
import org.json.JSONObject;

import com.chaoyong.higo.R;
import com.chaoyong.higo.adapter.GetGoodsAdapter;
import com.chaoyong.higo.adapter.PurchaseRecordAdapter;
import com.chaoyong.higo.base.BaseFragment;
import com.chaoyong.higo.bean.GetAllGoodsBean_z;
import com.chaoyong.higo.bean.GetgoodsAllBean;
import com.chaoyong.higo.net.OApi;
import com.chaoyong.higo.utils.EmptyUtil;
import com.chaoyong.higo.utils.JLog;
import com.chaoyong.higo.utils.PrefUtils;
import com.chaoyong.higo.utils.V;
import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

/**
 * 获得商品-已完成
 * 
 * @author zk
 *
 */
public class FragGetGoods3 extends BaseFragment {
	
	private static final String ClassName = FragGetGoods3.class.getName();

	private ListView order_list_view;
	private GetGoodsAdapter adapter;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

		mView = inflater.inflate(R.layout.frag_records, container, false);
		return mView;
	}

	@Override
	public void findView() {
		order_list_view = V.f(mView, R.id.order_list_view);
	}

	@Override
	public void init() {
		adapter = new GetGoodsAdapter(mContext);
		order_list_view.setAdapter(adapter);
	}
	
	@Override
	public void onStart() {
		super.onStart();
		String uid = PrefUtils.getString(mContext, "userId", "1");
		//TODO 测试时传入“1”，测试完成之后需要传入当前登录用户的id
		httpGetGoods(uid, "15000","2");
	}
	
	/**
	 * 获取待收货商品
	 * limit为显示的条数,status 0为待收货 1 为确认收货，2为已完成 2为待晒单
	 * @param uid
	 * @param limit
	 * @param status
	 */
	private void httpGetGoods(String uid, String limit,String status) {
		HttpUtils utils = new HttpUtils();
		RequestParams param = new RequestParams();
		try {
			JSONObject jsonObject = new JSONObject();
			JSONObject info = new JSONObject();

			jsonObject.putOpt("model", "GoodsWinning");
			jsonObject.putOpt("function", "luckyRecordApp");
			jsonObject.putOpt("info", info);

			info.putOpt("uid", uid);
			info.putOpt("limit", limit);
			info.putOpt("status", status);

			param.setBodyEntity(new StringEntity(jsonObject.toString(), "UTF-8"));
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		utils.send(HttpMethod.POST, OApi.Appport_appMember, param, new RequestCallBack<String>() {

			@Override
			public void onFailure(HttpException arg0, String arg1) {
				JLog.i(ClassName, arg0.getMessage());
			}

			@Override
			public void onSuccess(ResponseInfo<String> arg0) {
				JLog.i(ClassName, arg0.result);
				Gson gson = new Gson();
				GetgoodsAllBean bean = gson.fromJson(arg0.result, GetgoodsAllBean.class);
				String status = bean.getStatus();
				if (status.equals("1")) {
					List<GetgoodsAllBean.DataEntity.ListEntity> entity = bean.getData().getList();
					if (!EmptyUtil.isEmpty(entity)) {
						adapter.setList(entity);
					}
//					showToast(entity.size() + "");
				}
			}
		});
	}

}
